<template>
  <div class="ai-intention">
    <div class="line5 ai-intention-title">
      <div class="flow-dot flow-exec" magnet="true" x-type="input-exec" :x-id="'input-exec_'+id"></div>
      <div>{{ formData.name }}</div>
    </div>
    <div>
      <div class="line5">
        <div class="flow-dot flow-string" magnet="true" x-type="input-text" :x-id="'input-text_'+id"></div>
        <div>用户输入</div>
      </div>
      <div class="line5">
        <div class="flow-dot flow-array" magnet="true"  x-type="input-context" :x-id="'input-context_'+id"></div>
        <div>上下文</div>
      </div>
    </div>
    <el-form
      :model="formData"
      label-position="top"
      @mousedown.stop
      @focus.stop
      style="cursor: default"
    >
      <el-form-item label="节点名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="模型" prop="modelId">
        <el-select v-model="formData.modelId" placeholder="请选择模型" clearable>
          <el-option
            v-for="model in models"
            :key="model.id"
            :label="model.name"
            :value="model.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="addIntention">新增意图</el-button>
      </el-form-item>
    </el-form>
    <div class="ai-intention-input">
      <div class="line5">
        <div>默认意图</div>
        <div class="flow-dot flow-right flow-exec" magnet="true" x-type="output-intention" x-key="默认意图" :x-id="`output-intention:default_+${id}`"></div>
      </div>
      <template v-for="(ins,index) in formData.intentions" :key="index">
        <div class="line5">
          <el-input v-model="ins.name" />
          <div class="flow-dot flow-right flow-exec" magnet="true" x-type="output-intention" :x-key="ins.name" :x-id="`output-intention:${index}_+${id}`"></div>
        </div>
      </template>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ModelApi, ModelVO } from '@/api/ai/model/model'
import { AiModelTypeEnum } from '@/views/ai/utils/constants'
import {inject, ref} from "vue";
import {Node} from "@antv/x6";
const formData = reactive<any>({
  name: '意图分析',
  intentions:[]
})
const models = ref([] as ModelVO[])
const init = async () => {
  models.value = await ModelApi.getModelSimpleList(AiModelTypeEnum.CHAT)
}
const addIntention=()=>{
  formData.intentions.push({
    name:"",
  })
}
const id=ref<string>()
const getNode=inject<()=>Node>('getNode')
if(getNode){
  const node=getNode()
  id.value=node.id
}
onBeforeMount(()=>{
  if(getNode){
    const node=getNode()
    const d=node.getData()
    if (d){
      Object.assign(formData,d)
    }else {
      node.setData(formData)
    }
  }
})
onMounted(() => {
  init()
})
watch(formData,()=>{
  if(getNode){
    const node=getNode()
    node.setData(formData)
  }
},{deep:true,immediate:false})
</script>
<style scoped lang="scss">
.ai-intention {
  width: 400px;
  background-color: white;
  display: flex;
  padding: 10px;
  flex-direction: column;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid #9f9f9f;
  gap: 5px;
  .ai-intention-title {
    height: 32px;
  }
  .ai-intention-input{
    display: flex;
    flex-direction: column;
    gap: 5px;
  }
}
</style>
